<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              :disabled="singleNew"
              v-hasPermi="['project:gclList:add']"
            >
              新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['project:gclList:edit']"
            >
              修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['project:gclList:remove']"
            >
              删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              :loading="exportLoading"
              @click="handleExport"
              v-hasPermi="['project:gclList:export']"
            >
              导出
            </el-button>
          </el-col>
          <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
            <Page
              style="margin-right: 0px"
              :current="queryParams.pageNum"
              :page-size="queryParams.pageSize"
              :total="total"
              @on-change="pageChange"
              simple
            ></Page>
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>
        <el-table
          v-loading="loading"
          :data="gclListList"
          @row-click="clickData"
          border
          stripe
          :header-cell-style="{ 'text-align': 'center' }"
          highlight-current-row
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column
            label="开始值g"
            width="100"
            align="center"
            prop="gclKsval"
          />
          <el-table-column
            label="结束值g"
            width="100"
            align="center"
            prop="gclJsval"
          />
          <el-table-column
            label="公差值%"
            width="100"
            align="center"
            prop="gclRate"
          />
          <el-table-column
            label="公差值g"
            width="100"
            align="center"
            prop="gclGram"
          />
          <el-table-column
            label="备注"
            show-overflow-tooltip
            align="center"
            prop="gclDesc"
          />
          <el-table-column
            label="建立人员"
            width="120"
            align="center"
            prop="createBy"
          />
          <el-table-column label="建立日期" align="center" prop="createTime">
            <template #default="scope">
              <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="更改人员" align="center" prop="updateBy" />
          <el-table-column label="更改日期" align="center" prop="updateTime">
            <template #default="scope">
              <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="130"
            fixed="right"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['project:gclList:remove']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <!-- 添加或修改定额公差维护对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="680px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="24">
          <el-form-item label="客户代码" prop="gclKhdm">
            <el-input v-model="allName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始值g" prop="gclKsval">
            <el-input v-model="form.gclKsval" placeholder="请输入开始值g" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束值g" prop="gclJsval">
            <el-input v-model="form.gclJsval" placeholder="请输入结束值g" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公差值" prop="gclInput">
            <el-input v-model="form.gclInput" placeholder="请输入公差值" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公差类型" prop="gclState">
            <RadioGroup v-model="form.gclState">
              <Radio label="1">%</Radio>
              <Radio label="2">G</Radio>
            </RadioGroup>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="gclDesc">
            <el-input v-model="form.gclDesc" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGclList,
  getGclList,
  delGclList,
  addGclList,
  updateGclList,
  exportGclList,
  treeSelect,
  listGclListById,
} from "@/api/project/gclList";

export default {
  name: "GclList",
  data() {
    return {
      //传过去的名字到新增那里
      allName: "",
      allNamed: [],
      allNamedd: "",
      allId: null,
      dateRange: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 部门树选项
      deptOptions: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      //新增
      singleNew: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 根据名称筛选部门树
      deptName: null,

      // 定额公差维护表格数据
      gclListList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined,
        gclKhdm: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getTreeSelect();
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  methods: {
    getTreeSelect() {
      treeSelect().then((response) => {
        this.deptOptions.push(response.data);
      });
    },
    /** 查询定额公差维护列表 */
    getList() {
      this.loading = true;
      const tableH = (document.body.clientHeight - 160) / 2;
      this.queryParams.pageSize = tableH > 350 ? 25 : 15;
      listGclList(this.queryParams).then((response) => {
        this.gclListList = response.rows;
        this.total = response.total;

        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gclId: null,
        gclKhdm: null,
        gclKsval: null,
        gclJsval: null,
        gclRate: null,
        gclGram: null,
        gclDesc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        gclState: "1",
        gclInput: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.allName = data.label;
      this.allId = data.id;
      if (this.allName != "所有") {
        this.singleNew = false;
      } else {
        this.singleNew = true;
      }

      if (data.id === 0) {
        this.queryParams.gclKhdm = null;
      } else {
        this.queryParams.gclKhdm = data.id;
      }
      listGclListById(this.queryParams).then((response) => {
        this.gclListList = response.rows;
        this.total = response.total;

        this.loading = false;
      });
    },
    clickData(a) {
      this.allNamedd = a.gclKhmc;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.gclId);
      this.allNamed = selection.map((item) => item.gclKhmc);
      this.allNamedd = this.allNamed[0];
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加定额公差维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gclId = row.gclId || this.ids;
      getGclList(gclId).then((response) => {
        this.form = response.data;
        if (this.form.gclRate) {
          this.form.gclInput = this.form.gclRate;
          this.form.gclState = "1";
        } else if (this.form.gclGram) {
          this.form.gclInput = this.form.gclGram;
          this.form.gclState = "2";
        }
        this.open = true;
        this.title = "修改定额公差维护";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.gclKhdm = this.allId;
          if (this.form.gclState == "1") {
            this.form.gclRate = this.form.gclInput;
          } else if (this.form.gclState == "2") {
            this.form.gclGram = this.form.gclInput;
          }
          if (this.form.gclId != null) {
            updateGclList(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGclList(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gclIds = row.gclId || this.ids;
      this.$confirm(
        '是否确认删除定额公差维护编号为"' + gclIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delGclList(gclIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有定额公差维护数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportGclList(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
